package com.czwj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.czwj.entities.BudgetItem;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.CapitalApply;
import com.czwj.entities.DataGridData;
import com.czwj.entities.FinishApply;
import com.czwj.entities.PlanMain;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.ProjectItemChild;
import com.czwj.entities.UrgentApply;
import com.czwj.entities.UserInfo;
import com.czwj.entities.WarningValue;
import com.czwj.service.BudgetExecuteService;
import com.czwj.service.BudgetItemService;
import com.czwj.service.BudgetMainService;
import com.czwj.service.CapitalApplyService;
import com.czwj.service.FinishApplyService;
import com.czwj.service.PlanMainService;
import com.czwj.service.ProjectItemChildService;
import com.czwj.service.ProjectItemService;
import com.czwj.service.UrgentApplyService;
import com.czwj.service.WarningValueService;

@Controller
public class AssociationAction {
	@Resource
	ProjectItemService projectItemService;
	@Resource
	ProjectItemChildService projectItemChildService;
	@Resource
	PlanMainService planMainService;
	@Resource
	BudgetMainService budgetMainService;
	@Resource
	BudgetItemService budgetItemService;
	@Resource
	UrgentApplyService urgentApplyService;
	@Resource
	CapitalApplyService capitalApplyService;
	@Resource
	FinishApplyService finishApplyService;
	@Resource
	BudgetExecuteService budgetExecuteService;
	@Resource
	WarningValueService warningValueService;
	
	@RequestMapping(value = "associationprojectitems.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView projectitems(@RequestParam(value = "projectitemid", required = false) Integer id)  {
		ModelAndView mav = new ModelAndView();
		ProjectItem project = projectItemService.selectByPrimaryKey(id);
		mav.addObject("projectitem", project);
		mav.setViewName("myproject/association");
		return mav;
	}
	@RequestMapping(value = "associationprojectitemchilds.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView associationprojectitemchilds(@RequestParam(value = "projectitemchildid", required = false) Integer id)  {
		ModelAndView mav = new ModelAndView();
		ProjectItemChild project = projectItemChildService.selectByPrimaryKey(id);
		mav.addObject("projectitemchild", project);
		mav.setViewName("myproject/associationchild");
		return mav;
	}
	
	@RequestMapping(value = "/tomainplan.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> tomainplan(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<PlanMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<>();
		map.put("projectitemid", projectitemid);
		map.put("projectitemchildid", projectitemchildid);
		List<PlanMain> list = planMainService.searchbyprojectitem(map);
		result.setRows(list);
		return result;
	}
	
	@RequestMapping(value = "/tomainbudget.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetMain> tomainbudget(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<BudgetMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<>();
		map.put("projectitemid", projectitemid);
		map.put("projectitemchildid", projectitemchildid);
		List<BudgetMain> list = budgetMainService.searchbyprojectitem(map);
		result.setRows(list);
		return result;
	}
	@RequestMapping(value = "/toapplyurgent.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<UrgentApply> toapplyurgent(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<UrgentApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<>();
		map.put("projectitemid", projectitemid);
		map.put("projectitemchildid", projectitemchildid);
		List<UrgentApply> list = urgentApplyService.searchbyprojectitem(map);
		result.setRows(list);
		return result;
	}
	@RequestMapping(value = "/toapplycapital.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CapitalApply> toapplycapital(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<CapitalApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<>();
		map.put("projectitemid", projectitemid);
		map.put("projectitemchildid", projectitemchildid);
		List<CapitalApply> list = capitalApplyService.searchbyprojectitem(map);
		result.setRows(list);
		return result;
	}
	@RequestMapping(value = "/toapplyfinish.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<FinishApply> toapplyfinish(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<FinishApply> result = new DataGridData();
		List<FinishApply> list = finishApplyService.searchbyprojectitem(projectitemid);
		result.setRows(list);
		return result;
	}
	
	@RequestMapping(value = "/tomoneywarning.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Map<String, Object>> tomoneywarning(
			@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid) {
		DataGridData<Map<String, Object>> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map3 = new HashMap<String, Object>();
		List<Map<String, Object>> listMap=new ArrayList<Map<String, Object>>();
		map.put("projectitemid", projectitemid);
		List<BudgetItem> itemlist = budgetItemService.search(null);
		for (BudgetItem budgetItem : itemlist) {
			map.put("budgetitemid", budgetItem.getId());
			map3=budgetItemService.warningtips(map);
			if(map3!=null){ 
				if(Double.valueOf(map3.get("budgetmoney").toString())!=0){
					listMap.add(map3);
				}
			}
		}
		WarningValue warningValue=warningValueService.selectByPrimaryKey(2);
		List<Map<String, Object>> listMap2=new ArrayList<Map<String, Object>>();
		for(int k=0;k<listMap.size();k++){
			Double percent=Double.valueOf(listMap.get(k).get("executemoney").toString())/
					(Double.valueOf(listMap.get(k).get("budgetmoney").toString())+Double.valueOf(listMap.get(k).get("applymoney").toString()));
			
			if(percent*100>=warningValue.getPercent()){
				listMap2.add(listMap.get(k));
			}
		}
		result.setRows(listMap2);
		result.setTotal(listMap2.size());
		return result;
	}
	
	@RequestMapping(value = "/totimewarning.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> warningTime(@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid)throws Exception{
		DataGridData<PlanMain> result = new DataGridData();
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user=((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		Map<String, Object> map = new HashMap<>();
		map.put("projectitemid", projectitemid);
		List<PlanMain> planlist = planMainService.searchbyprojectitem(map);
		List<PlanMain> planmainlist=new ArrayList<PlanMain>();
		WarningValue warningValue=warningValueService.selectByPrimaryKey(1);
		for(int k=0;k<planlist.size();k++){
			Double percent=(double) (System.currentTimeMillis()-planlist.get(k).getStartdate().getTime())/
					(planlist.get(k).getEnddate().getTime()-planlist.get(k).getStartdate().getTime());
			
			if(percent*100>=warningValue.getPercent()){
				planmainlist.add(planlist.get(k));
			}
		}
		result.setRows(planmainlist);
		result.setTotal(planmainlist.size());
		return result;
	}
}
